<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取表单元素的值</title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            render(){
                return <form action="">
                        <h2>登录</h2>
                        用户名: <input type="text" ref={el => this.user = el} /><br/>
                        密码: <input type="password" ref={el => this.pass = el} /><br/>
                        <input type="submit" value="登录" onClick={this.login} />
                    </form>
            }

            //声明事件回调
            login = (e) => {
                let username = this.user.value;
                let password = this.pass.value;

                //输出
                console.log('用户名为: '+username, '密码为: ' + password);

                //清空表单的值
                this.user.value = '';
                this.pass.value = '';
                //阻止默认行为
                e.preventDefault();
            }
        }

        //渲染
        ReactDOM.render(<App/>, document.querySelector("#root"));
    </script>
</body>
</html>